package com.ddc.study.feature.main.project;

import android.text.Html;
import android.view.View;

import com.ddc.study.R;
import com.ddc.study.base.TabReselectFragment;
import com.ddc.study.entity.Tree;
import com.ddc.study.feature.main.project.detail.DetailFragment;
import com.google.android.material.tabs.TabLayout;

import java.lang.reflect.Field;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created on 2018/5/22.
 *
 * @author lshun
 * @version 1.0
 */
public class ProjectFragment extends TabReselectFragment<ProjectContract.IView, ProjectContract.IPresenter>
        implements ProjectContract.IView {
    @BindView(R.id.content_view)
    View mContentView;

    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;

    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    @Nullable
    private List<Tree> mTreeData;

    private MyPagerAdapter mPagerAdapter;

    @Override
    protected ProjectContract.IPresenter createPresenter() {
        return new ProjectPresenter();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.frag_project;
    }

    @Override
    protected void initView() {
        super.initView();
        if (getActivity() == null) return;

        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                onTabReselect();
            }
        });

        if (mTreeData == null) {
            //没有数据 加载数据
            loadData();
        } else {
            //已有数据  直接展示
            mViewPager.setAdapter(mPagerAdapter);
        }

        setRetainInstance(true);
    }

    @Override
    public void loadProjectTreeSuccess(@Nullable final List<Tree> data) {
        mTreeData = data;

        if (data == null || data.isEmpty()) {
            mFailedView.setVisibility(View.VISIBLE);
            mContentView.setVisibility(View.GONE);
        } else {
            mFailedView.setVisibility(View.GONE);
            mContentView.setVisibility(View.VISIBLE);

            if (mPagerAdapter == null) {
                mPagerAdapter = new MyPagerAdapter(getChildFragmentManager(), data);
                mViewPager.setAdapter(mPagerAdapter);
            } else {
                showToast("mPageAdapter is not null");
                mPagerAdapter.setNewData(data);
            }
        }
    }

    @Override
    public void loadProjectTreeFailed(String msg) {
        //加载失败
//        showToast(msg);
        mFailedView.setVisibility(View.VISIBLE);
        mContentView.setVisibility(View.GONE);
    }

    @BindView(R.id.view_load_failed)
    View mFailedView;

    @OnClick(R.id.view_load_failed)
    void loadData() {
        mPresenter.loadProjectTree();
    }

    @Override
    public void onTabReselect() {
        try {
            Field field = FragmentStatePagerAdapter.class.getDeclaredField("mFragments");
            field.setAccessible(true);
            List fragments = (List) field.get(mPagerAdapter);
            if (fragments == null) return;
            TabReselectFragment f = (TabReselectFragment) fragments.get(mViewPager.getCurrentItem());
            if (f == null) return;
            f.onTabReselect();
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    private static final class MyPagerAdapter extends FragmentStatePagerAdapter {
        private List<Tree> mData;

        void setNewData(@Nullable List<Tree> data) {
            mData = data;
            notifyDataSetChanged();
        }

        MyPagerAdapter(FragmentManager fm, @Nullable List<Tree> data) {
            super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
            mData = data;
        }

        @NonNull
        @Override
        public Fragment getItem(int i) {
            return DetailFragment.getInstance(mData.get(i));
        }

        @Override
        public int getCount() {
            return mData == null ? 0 : mData.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return Html.fromHtml(mData.get(position).getName());
        }
    }
}
